home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2056 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Examples of using "volatile"?
  5. Date: 18 Jan 1996 16:03:16 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan18090316@qcd.lanl.gov>
  8. References: <4djoj2$mr1@post.gsfc.nasa.gov>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Stephen Maher's message of 17 Jan 1996 21:09:54 GMT
  13.  
  14. --text follows this line--
  15. In article <4djoj2$mr1@post.gsfc.nasa.gov> Stephen Maher
  16. <Stephen.Maher@gsfc.nasa.gov> writes: 
  17. <snip>
  18.    I'd like a concrete example(s) illustrating a reason to
  19.    use the "volatile" type qualifier.
  20.  
  21. The first place where I used it is when I was doing memory mapped
  22. i/o: when I read or wrote to a `memory' location, it was interpreted
  23. by a periphereal element as a request to do some communication; or
  24. report the result of one.
  25.  
  26. I knew the `address' at which I had to read or write to. I knew how
  27. (using implementation defined features) I could create a pointer to
  28. this address. But, without volatile, the compiler always aggressively
  29. optimized two successive `reads' into one, or two successive `writes'
  30. into one. This of course, messed up the communication altogether :-)
  31.  
  32. Adding a volatile fixed it; and gave me one more reason to learn C. 
  33.  
  34.    I understand the *theory* behind volatile (e.g., to
  35.    avoid unwanted side-effects?).  I also realize it's
  36.    probably implementation dependent. But some examples
  37.    from any implementation should help me understand
  38.    its use a little better.
  39.  
  40. The above is probably the kind of example that you wanted. When I
  41. learnt C, I realized that there are situations involving
  42. setjmp/longjmp, where `volatile' is _required_ for a program to be
  43. portable. Naturally, such cases are _not_ implementation dependent.
  44.  
  45. Cheers
  46. Tanmoy
  47. --
  48. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  49. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  50. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  51. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  52. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  53. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  54.